package com.bang.study.service.impl;

import com.bang.study.common.Result;
import com.bang.study.common.ResultCode;
import com.bang.study.config.RabbitConfig;
import com.bang.study.entity.Mail;
import com.bang.study.entity.MsgLog;
import com.bang.study.mapper.MsgLogMapper;
import com.bang.study.mq.MessageHelper;
import com.bang.study.service.ITestService;
import com.bang.study.util.RandomUtil;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName: TestServiceImpl
 * @author: BANG
 * @Date: 2020/01/16 11:58
 * @Version: 1.0
 * @Modified By:
 * @Desc: TODO
 */
@Service
public class TestServiceImpl implements ITestService {


    @Autowired
    private MsgLogMapper msgLogMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public Result send(Mail mail) {
        String msgId = RandomUtil.UUID32();
        mail.setMsgId(msgId);

        MsgLog msgLog = new MsgLog(msgId, mail, RabbitConfig.MAIL_EXCHANGE_NAME, RabbitConfig.MAIL_ROUTING_KEY_NAME);
        msgLogMapper.insert(msgLog);// 消息入库

        CorrelationData correlationData = new CorrelationData(msgId);
        rabbitTemplate.convertAndSend(RabbitConfig.MAIL_EXCHANGE_NAME, RabbitConfig.MAIL_ROUTING_KEY_NAME, MessageHelper.objToMsg(mail), correlationData);// 发送消息

        return   new Result<>().setCode(ResultCode.SUCCESS);
    }
}
